/**
 * 班级管理器
 *
 */
function ClassListCtrl($scope, $http, $state) {
    $scope.data = {};
    $scope.rows = [];
    $http({
        url: "class/findAll",
        method: 'get',
    }).success(function (rows) {
        $scope.rows = rows;
    });
    //添加
    $scope.add = function () {
        $scope.data = {
            start_time: '',
            comment: '',
            subject: '',
            title: ''
        };
    }
    //编辑
    $scope.edit = function (id) {
        for (var i in $scope.rows) {
            var row = $scope.rows[i];
            if (id == row.id) {
                $scope.data = row;
                return;
            }
        }
    }
    //保存
    $scope.save = function () {
        $http({
            url: 'class/save',
            method: 'POST',
            data: $scope.data
        }).success(function (r) {
            //保存成功后更新数据
            $scope.get(r.id);
            $('.close').click();
            alert("添加成功!");
            $state.go('index');
        });
    }

    $scope.del = function (id) {
        $http({
            url: 'class/delete?number=' + id,
            method: 'POST',
        }).success(function (r) {
            //删除成功后移除数据
            $scope.remove(r.id);
        });
    }

    $scope.selected = [];
    //获取数据
    $scope.get = function (id) {
        $http({
            url: 'class/get?id=' + id,
            method: 'POST',
        }).success(function (data) {
            for (var i in $scope.rows) {
                var row = $scope.rows[i];
                if (data.id == row.id) {
                    row.title = data.title;
                    row.subject = data.subject;
                    row.start_time = data.start_time;
                    row.status = data.status;
                    return;
                }
            }
            $scope.rows.push(data);
        });
    }
    $scope.deleteItems = function() {
        var checkedlist = new Array();
        angular.forEach($scope.rows, function(x) {
            if (x.$checked == true)
                checkedlist.push(x.id);
        });
        $http({
            url : "class/deleteItems",
            method : "post",
            params : {
                'ids' : checkedlist,
            }
        }).success(function(response) {
            $scope.rows = rows;

        });
    };

    $scope.getClassByStatus = function (id) {
        $scope.id = id;
        $http({
            url: 'dept/add',
            method: 'post',
            params: {
                id: $scope.id
            }
        }).success(function (response) {
            $state.go('index.organzationManage', location.reload('index.organzationManage'));
        })
    }
    $scope.find = function () {
        //$('.close').click();
        $state.go('feedbackson');
        //alert("hsudhsaudhsau");
    }

    $scope.retreat = function () {
        //$('.close').click();
        $state.go('feedback');
        //alert("hsudhsaudhsau");
    }
    $scope.addHomework = function () {
        //$('.close').click();
        $state.go('addHomework');
        //alert("hsudhsaudhsau");
    }

    $scope.checkAll = function (checked) {
        angular.forEach($scope.rows, function (x) {
            x.$checked = checked;
        });
    };

    $scope.findAllByStatus = function (x) {
        $http(
            {
                url: 'class/findAllByStatus',
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: 'status=' + x
            }).success(function (rows) {
            $scope.rows = rows;
        });
    };
    $scope.findAll = function () {
        $http(
            {
                url: 'class/findAll',
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: 'status=' + 0 || 'status=' + 1 || 'status=' + 2
            }).success(function (rows) {
            $scope.rows = rows;
        });
    };

}
/**
 * 作业控制器
 * @param $scope
 * @param $http
 * @constructor
 */
function HomeworkCtrl($scope, $http, $state, $stateParams) {

    $scope.id = $stateParams.id;

    $http({
        url: "homework/findAll",
        method: 'get',

    }).success(function (response) {
        $scope.classes = response;
    });

}


function FindClassByStatus($scope, $http) {
    $http({
        url: "class/findAllByStatus",
        method: "get",
    }).success(function (response) {
        $scope.classes = response;
    })
}

function LoginCtrl($scope, $http, $state) {
   // $scope.wrong = false;
    $scope.user = null;
    $scope.login = function () {

        $http({
            url: 'user/login',
            method: 'get',
            params: $scope.user
        }).success(function (response) {
            if (response == "登录成功") {
                $state.go('Commodity');
            } else {
                $scope.wrong = true;
                alert("登录失败，请重新登录");
                $state.go('login');
            }
        })
    }

}
function RegisterCtrl($scope, $http,$state) {//查了这么长时间bug，才发现这里忘记声明state了
    $scope.user = null;
    $scope.register = function () {
        $http({
            url: 'user/register',
            method: 'get',
            params: $scope.user
        }).success(function (response) {
            console.log("response+" + response);
            var message=response.toString();
            console.log("message+"+message)
            if (message == "注册成功") {
                alert("注册成功");
                $state.go('login');
            } else {
                alert("注册失败，请重新注册");
                $state.go('register');
            }
            $scope.classes = response;
        })
    }
}

function CommodityCtrl($scope,$state,$http){
    $scope.rows=[];
    $http({
      url:'Commodity/findAll',
      method:'get'
    }).success(function (rows) {
        console.log(rows);
        $scope.rows=rows;//这里的rows必须定义
    });
   $scope.CommodityDetail=function (commodityId) {
        localStorage.setItem("commodityId",commodityId);
        $state.go("selectedCommodityDetail");
   }
}
function SelectedCommodityDetailCtrl($scope,$state,$http){
    var commodityId=localStorage.getItem("commodityId");
    $scope.selectednum=1;
    console.log("commodityId"+commodityId);
    $http({
        url:'Commodity/findById',
        method:'get',
        params:{
            CommodityId:commodityId
        }
    }).success(function (response) {
        $scope.commodity=response;
    })
    $scope.decrease=function () {
        if(this.selectednum=1){
            this.selectednum=1;
        }else{
            this.selectednum-=1;
        }
    }
    $scope.add=function () {
        this.selectednum+=1;
    }

    $scope.buy=function(){
        console.log("被选择的数量为+"+this.selectednum)
        console.log("被选择商品的id为+"+commodityId)
        $http({
            url:'Commodity/buy',
            method:'get',
            params:{
                num:this.selectednum,
                commodityId:commodityId
            }
        }).success(function (response) {

            if(response=="true") {
                alert("购买成功");
                $state.go("Commodity")
            }
        })
    }
    
    
}
function AddHomeworkCtrl($scope, $http) {
    $http({
        url: "question/findAll",
        method: "get",
    }).success(function (response) {
        $scope.classes = response;
    })
}

function StudentCtrl($scope, $http, $state) {
    $http({
        url: "student/findAll",
        method: 'get',
    }).success(function (rows) {
        $scope.rows = rows;
    });

    $scope.checkAll = function (checked) {
        angular.forEach($scope.rows, function (x) {
            x.$checked = checked;
        });
    };
    $scope.data = {};
    $scope.rows = [];

    //添加
    $scope.add = function () {
        $scope.data = {
            student_name: '',
            student_no: '',
            start_time: '',
            comment: '',
            parent_tel: '',
            parent_wechat: ''
        };
    }

    //保存
    $scope.save = function () {
        $http({
            url: 'student/save',
            method: 'POST',
            data: $scope.data
        }).success(function (r) {
            //保存成功后更新数据
            $scope.get(r.id);
            $('.close').click();
            alert("添加成功!");
            $state.go('class');
        });
    }

    // $scope.del = function (id) {
    // 	$http({
    // 		url: 'student/delete?id=' + id,
    // 		method: 'POST',
    // 	}).success(function (r) {
    // 		//删除成功后移除数据
    // 		$scope.remove(r.id);
    // 	});
    // }

    //获取数据
    $scope.get = function (id) {
        $http({
            url: 'student/get?id=' + id,
            method: 'POST',
        }).success(function (data) {
            for (var i in $scope.rows) {
                var row = $scope.rows[i];
                if (data.id == row.id) {
                    row.student_no = data.student_no;
                    row.parent_tel = data.parent_tel;
                    row.start_time = data.start_time;
                    row.parent_wechat = data.parent_wechat;
                    row.student_name = data.student_name;
                    return;
                }
            }
            $scope.rows.push(data);
        });
    }
    $scope.checkAll = function (checked) {
        angular.forEach($scope.rows, function (x) {
            x.$checked = checked;
        });
    };
}